const defaultOption = {
    header: true,
    footer: true,
    landscape: false,
    size: '',
}
/**
 * 打印核心
 * @param {string} content 
 */
function printcore (content) {
    
    const iframe = document.createElement('iframe');
    iframe.width=1024

    iframe.style.display = 'none';
    document.body.appendChild(iframe);
    // console.log("%c 你可以的", 'color:#ee7', content)
    const doc = iframe.contentWindow.document;
    doc.write(content);
    doc.close();
    
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
    iframe.contentWindow.addEventListener("afterprint", () => {
        document.removeChild(iframe);
    })
}
/**
 * 打印html
 * @param {string} html 
 * @param {boolean} opt.header 
 * @param {boolean} opt.footer 
 */
export function printHTML(html, opt={}) {
    const options={
        ...defaultOption
    };

    if (opt) {
        Object.assign(options, opt)
    }
    let parser = new DOMParser()
    const doc = parser.parseFromString(html,'text/html');
    let pageStyle = ''
    let sizeOpt = '';
    if (!options.header) {
        pageStyle= `${pageStyle}margin-top: 0;`
    }
    if (!options.footer) {
        pageStyle= `${pageStyle}margin-bottom: 0;`
    }
    if (options.size) {
        sizeOpt= options.size;
    } 
    if (options.landscape) {
        sizeOpt = `${sizeOpt} landscape`;
    }
    const sizeText = sizeOpt?`size: ${sizeOpt};`:"";
    pageStyle = `@page {${pageStyle}${sizeText}}`
    const styleEle = document.createElement("style")
    styleEle.innerText = pageStyle;
    doc.head.append(styleEle)
    const serializer = new XMLSerializer();
    const htmlString = serializer.serializeToString(doc);
    // console.log(htmlString);
    printcore(htmlString)
}

export function printNode(node, opt={}) {
    const style = getComputedStyle(node);
    console.log("咩", style)
}
export default printcore;